<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\Report;

class OutFlowCountFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'out:count-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '出口流量统计-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $this->_mkdata(intval(date('n')) - 1);

        // for ($i = -10; $i <= -1; $i++) { 
        //     $this->_mkdata($i);
        // }
    }

    private function _mkdata($month = 0)
    {
        $start = mktime(0, 0, 0, $month, 1, intval(date('Y')));
        $end   = mktime(0, 0, 0, $month+1, 1, intval(date('Y')));
        $report_date = date('Y-m', $start);

        $this->info('start: '. date('Y-m-d H:i:s', $start));
        $this->info('end:   '. date('Y-m-d H:i:s', $end - 1));

        $auth =array_flip(config('custom.auth_type'));
        $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
            ->whereNotIn('park_code', config('custom.taxi_parks'))
            ->select(['park_code', 'parking_name'])
            ->orderBy('sort_by', 'desc')
            ->get();
        $data = $parks->map(function ($item, $key) use ($start, $end, $auth) {
            //查询前一天数据
            $table = 't_park_record_' . $item->park_code;
            $parkRecord = new ParkRecord();
            $parkRecord->setTable($table);
            $res = $parkRecord
                ->groupBy(['out_arm_code','park_code'])
                ->where('out_time', '>=', $start)
                ->where('out_time', '<', $end)
                ->select([
                    \DB::raw('COUNT(*) as flow'),
                    'park_code',
                    'out_arm_code'
                ])->get();

            return $res;
        });
        $taxi_data = $parks->map(function ($item, $key) use ($start, $end, $auth) {
            //查询前一天数据
            $table = 't_park_record_' . $item->park_code;
            $parkRecord = new ParkRecord();
            $parkRecord->setTable($table);
            $res = $parkRecord
                ->groupBy(['out_arm_code','park_code'])
                ->where('out_time', '>=', $start)
                ->where('out_time', '<', $end)
                ->select([
                    \DB::raw('COUNT(*) as flow'),
                    'park_code',
                    'out_arm_code'
                ])->get();
            return $res;
        });
        $data = array_merge($data->toArray(), $taxi_data->toArray());
        //存入数据库

        if (!$report = Report::where('type', 'out_count_day_report')
                                ->where('report_date', $report_date)
                                ->first()) {
            $report = new Report;
            $report->report_date = $report_date;
            $report->type = 'out_count_day_report';
        }

        $report->value = $data;
        $report->save();

        $this->info('出口流量统计完成');
    }
}
